package com.jy.rhin.application.cda;

import com.alibaba.fastjson.JSONObject;
import com.jy.api.upload.UploadAPI;
import com.jy.framework.base.utils.Serializer;
import com.jy.rhin.facade.cda.dto.QianZhiJDPZWJDto;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.sdk.dto.document.query.DocumentValidateResultDto;
import com.jy.rhin.utils.FileUtil;
import com.jy.starter.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

@Service
@Slf4j
public class QianZhiJDApplication {

    @Autowired
    private UploadAPI uploadAPI;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CdaApplication cdaApplication;

    private static final String QIANZHIJIEDIANKEY = "CDA-QIANZHIJIEDIAN-PEIZHI";

    public QianZhiJDPZWJDto getPeiZhi() {
        QianZhiJDPZWJDto qianZhiJDPZWJDto;
        Object ob = redisUtil.get(QIANZHIJIEDIANKEY);
        if (ob == null) {
            qianZhiJDPZWJDto = new QianZhiJDPZWJDto();
            redisUtil.set(QIANZHIJIEDIANKEY, JSONObject.parseObject(Serializer.serialize(qianZhiJDPZWJDto), QianZhiJDPZWJDto.class));
        }
        qianZhiJDPZWJDto = Serializer.deserialize(JSONObject.toJSONString(ob), QianZhiJDPZWJDto.class);
        return qianZhiJDPZWJDto;
    }

    public boolean updateConfig(QianZhiJDPZWJDto dto) {
        String qianZhiJD = Serializer.deserialize(Serializer.serialize(dto)).toJSONString();
        QianZhiJDPZWJDto jsonObject = JSONObject.parseObject(qianZhiJD, QianZhiJDPZWJDto.class);
        return redisUtil.set(QIANZHIJIEDIANKEY, jsonObject);
    }

    public DocumentValidateResultDto jiaoYanWD(String ossUrl) {
//        String aliNetwork = uploadAPI.getAliNetwork(ossUrl);
        String cdaStr = null;
//        try (InputStream inputStream = FileUtil.getInputStream(aliNetwork)) {
        try (InputStream inputStream = FileUtil.getInputStream(ossUrl)) {
            cdaStr = convertInputStreamToString(inputStream);
        } catch (Exception e) {
            log.error("cda读取文件失败: {}", ExceptionUtils.getFullStackTrace(e));
            throw new CDAException("url 读取文件失败!", new RuntimeException());
        }
        try {
            return cdaApplication.validate(cdaStr);
        } catch (Exception e) {
//            log.error("cda文档解析出错: {}", ExceptionUtils.getFullStackTrace(e));
//            throw new CDAException("cda文档解析出错!", new RuntimeException());
            log.error(e.getMessage(), ExceptionUtils.getFullStackTrace(e));
            throw new CDAException(e.getMessage(), new RuntimeException());
        }
    }

    protected   String convertInputStreamToString(InputStream inputStream) throws IOException {
        Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
        String result = scanner.hasNext() ? scanner.next() : "";
        scanner.close();
        return result;
    }
}
